<template>
	<view class="">
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<view class="search-title">
			<text>搜索历史</text>
			<uni-icons type="trash-filled" size="20" @click="clearhistory"></uni-icons>
		</view>
		<view class="search-keyword" v-for="(item,index) in searchResults" :key="index" v-if="searchResults.length!==0" @click="gotoDetail(item.goods_id)">
			<view class="search-keyword-text">{{item.goods_name}} </view><uni-icons type="forward"></uni-icons>
		</view>
		<view class="search-history" v-if="searchResults.length===0">
			<view @click="gotoGoodsList(item)" v-for="(item,index) in searchHistory" :key="index">{{item}}<uni-icons class="closeempty" type="closeempty" size="" @click="deleteSearchRecordsItem(index)"></uni-icons></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//演示器timeId
				timer:null,
				kw:'', //搜索关键字
				searchResults:[],//搜索结果
				searchHistory:[]
			}
		},
		onLoad() {
			this.searchHistory=JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(e){
				//清除延时器
				clearTimeout(this.timer)
				//重新启动一个延时器，并把timerId赋值给this.timer
				this.timer = setTimeout(()=>{
					this.kw = e
					// console.log(this.kw);
					this.getSearchList()
					if(e!==''){
						const set=new Set(this.searchHistory)
						set.add(this.kw)
						this.searchHistory=Array.from(set)
						this.searchHistory.reverse()
						uni.setStorageSync('kw',JSON.stringify(this.searchHistory))
					}
				},500)
			},
			async getSearchList(){
				//判断关键字是否为空
				if(this.kw===''){
					this.searchResults=[]
					return
				}
				const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(res.meta.status !==200) return uni.$showMsg()
				this.searchResults=res.message
				
			},
			gotoDetail(goods_id){
				uni.navigateTo({
					url:`/subpkg/googs_detail/googs_detail?goods_id=${goods_id}`
				})
			},
			clearhistory(){
				let his = this.searchHistory
				uni.showModal({
				 content: '是否确认删除',
				 success: function(res) {
				  if (res.confirm) {
				   his.splice(0,his.length)
				   uni.removeStorageSync('kw')
				  } else if (res.cancel) {
				   console.log('用户点击取消');
				  }
				 }
				});
			},
			deleteSearchRecordsItem(index) {
			    const arr = this.searchHistory
			    uni.showModal({
			     content: '是否确认删除',
			     success: function(res) {
			      if (res.confirm) {
			       arr.findIndex((item,id,arr)=>{
					   if(id===index){
						   arr.splice(id,1)
					   }
				   })
			      } else if (res.cancel) {
			       console.log('用户点击取消');
			      }
			     }
			    });
			   },
			   //点击搜索历史跳转到商品页表
			   gotoGoodsList(item){
				   uni.navigateTo({
				   	url:`/subpkg/goods_list/goods_list?query=${item}`
				   })
			   }
		}
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
	z-index: 999;
}
.search-keyword{
	line-height: 40px;
	
	margin: 0 10px;
	border-bottom: 1px solid #ccc;
	display: flex;
}
.search-keyword-text{
	white-space: nowrap;
	text-overflow:ellipsis;
	overflow: hidden;
}
.search-title{
	display: flex;
	justify-content: space-between;
	margin: 0 5px;
	height: 30px;
	line-height: 30px;
	border-bottom: 1px solid darkgray;
}
.search-history{
	margin-top: 10px;
	
	view{
		margin: 5px;
		padding: 5px;
		padding-right: 30px;
		background-color: #ccc;
		display: inline-block;
		position: relative;
		border-radius: 10px;
		.closeempty{
			position: absolute;
			top: 0;
			right: 0;
		}
	}
}
</style>
